//router/index.ts
import {createRouter, createWebHistory} from "vue-router";
import layout from "../views/layout/layout.vue"
import store from "../store/index.js"


// 路由配置
const routes = [
    {
        path: '/login',
        name: "login",
        component: () => import("../views/pages/login.vue")
    },
    {
        path: '/',
        name: "layout",
        component: layout,
        redirect: "/index",
        // 子路由/嵌套路由
        children: [
            {
                path: '/index',
                name: "index",
                component: () => import("../views/pages/index.vue")
            },
            {
                path: "/roles",
                name: "roles",
                component: () => import("../views/pages/rolesList.vue")
            },
            {
                path: "/user",
                name: "user",
                component: () => import("../views/pages/usersList.vue")
            },
            {
                path: "/goods",
                name: "goods",
                component: () => import("../views/pages/goodsList.vue")
            },
            {
                path: "/form",
                name: "form",
                component: () => import("../views/pages/form.vue")
            }
        ]

    }

];

export const router = createRouter({
    history: createWebHistory(),
    routes,
});

//router 路由前置，路由拦截器
router.beforeEach((to,form,next)=>{
    /**
     * to:从哪个页面
     * from:到哪个页面
     * next:只有执行next()页面才会进行跳转
     */
    // 判断用户是否登录
    console.log("store",store.state.userInfo)
    const uInfo = store.state.userInfo.userInfo
    if(!localStorage.getItem('userInfo')){
        // 未登录,跳转到login
        if(to.path==="/login"){
            next()
            return
        }
        next("/login")

    }else{

        next()
    }
})

// 暴露路由对象
export default router






